package cn.itsource.service.blog.imp;

import cn.hutool.core.util.StrUtil;
import cn.itsource.blog.domain.article.Article;
import cn.itsource.blog.domain.article.ArticleDto;
import cn.itsource.blog.domain.article.CountTime;
import cn.itsource.blog.domain.article.DivTime;
import cn.itsource.blog.query.article.ArticleQuery;
import cn.itsource.head.ColPic;
import cn.itsource.head.PiePic;
import cn.itsource.mapper.blog.ArticleMapper;
import cn.itsource.service.blog.IArticleService;
import cn.itsource.upload.service.MinioIService;
import cn.itsource.utils.JsonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
@Service
public class ArticleImplService implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private MinioIService minioIService;

    @Override
    public List<Article> selectAll() {
        return articleMapper.selectAll();
    }

    @Override
    public void delete(Integer id) throws ServerException, InvalidBucketNameException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        Article article = this.get(id);
        //获取图片名
        String articlePic = article.getArticlePic();
        //如果图片不为空
        if(StrUtil.isNotBlank(articlePic)){
            //获取路径最后一根/对应的索引
            int index = articlePic.lastIndexOf("/");
            //获取附件名
            String fileName = articlePic.substring(index+1);
            //删除文件名
            minioIService.delete(fileName);
        }
        //删除数据
        articleMapper.delete(id);
    }

    @Override
    public Article get(Integer id) {
        return articleMapper.get(id);
    }

    @Override
    public void update(Article article) {
        if (article.getId() == null) {
            articleMapper.add(article);
            JsonResult.success("新增用户数据成功！");
        } else {
            articleMapper.update(article);
            JsonResult.success("修改成功");
        }
    }


    @Override
    public void deleteMore(Long[] ids) {
        articleMapper.deleteMore(ids);
    }

    @Override
    public PageInfo<Article> queryArticle(ArticleQuery articleQuery) {
        PageHelper.startPage(articleQuery.getCurrentPage(), articleQuery.getPageSize());
        List<Article> articles = articleMapper.queryArticle(articleQuery);
        return new PageInfo<>(articles);
    }

    @Override
    public Article selectOne(Integer id) {
        return articleMapper.selectOne(id);
    }

    @Override
    public Article selectDepend(Integer id) {
        return articleMapper.selectDepend(id);
    }

    @Override
    public List<ArticleDto> selectDto() {
        return articleMapper.selectDto();
    }

    @Override
    public List<CountTime> selectTime() {
        return articleMapper.selectTime();
    }

    @Override
    public List<PiePic> selectPie() {
        return articleMapper.selectPie();
    }

    @Override
    public List<ColPic> selectCol() {
        return articleMapper.selectCol();
    }

    @Override
    public List<DivTime> selectDiv(CountTime countTime){
        return articleMapper.selectDiv(countTime);
    }

    @Override
    public void readUp(Article article){

        articleMapper.readUp(article);
    }


}
